<?php
namespace Admin\Controller;
use Think\Controller;
class coachController extends Controller {

    //显示用户列表方法
    public function coachlistJson(){
       
        $use= $this->caoch=M('coach')->select();
        echo json_encode($use);
      } 
    public function coachList() {
        $coachModel = M('coach');
        $coachList = $coachModel -> select();
  echo "$coachList";
        $this -> assign('coachList',$coachList);
        $this -> display();
    }
    public function doAddcoach() {
          $coachModel = M('coach');
        //上传图片
        if ($_FILES['coachpic']['error'] == 0) {
            $imageInfo = $this->upload();
            $pname = $imageInfo['coachpic']['savename'];
        } else {
            $pname = "default.jpg";
        }

        $addcoachData['coachid'] = NULL;
        $addcoachData['coachname'] = I('post.coachname');
         $addClubData['coachpic'] = $pname;
        $addcoachData['coachtext'] = I('post.coachtext');
        $addcoachData['flag'] = 1;

        $res = $coachModel -> add($addcoachData);

        if ($res) {
            $this -> success("添加成功!");
        } else {
            $this -> error("添加失败!");
        }
    }
      public function updatecoach($id){
        $coachModel = M('coach');
        $coachData['coachid'] = $id;
        $res = $coachModel -> where($coachData) -> find();
        $this -> assign('coachInfo', $res);
        $this -> display();
    }
    public function doupdatecoach() {
        $coachModel = M('coach');

        $updatecoachData['coachid'] = I('post.coachid');
        $updatecoachData['coachname'] = I('post.coachname');
        $updatecoachData['coachtext'] = I('post.coachtext');
        $updatecoachData['flag'] = 1;


        $res = $coachModel -> save($updatecoachData);
  if ($res) {
            $this -> success("修改成功!",U('coach/coachList'));
        } else {
            $this -> error("修改失败!");
        }
    }
      public function delcoach($id){
       $res= M("coach") -> where("coachid = '$id'") -> delete();
       if ($res) {
            $this -> success("删除成功!",U('coach/coachList'));
        } else {
            $this -> error("修改失败!");
        }
    }
    public function recommendcoach ($id, $flag) {
        $subFlag = 0;
        if ($flag == 1) {
            $subFlag = 0;
        } else {
            $subFlag = 1;
        }
        $coachData['flag'] = $subFlag;
        $res = M('coach') -> where("coachid = '$id'") -> save($coachData);
        if ($res) {
            $this -> success("更改成功!",U('coach/coachList'));
        } else {
            $this -> error("更改失败!");
        }
    }

    

    //  public function index(){
    //     $share = M('share');
    //     //用户名条件搜索
    //     if(!empty($_GET['name'])){
    //       $where['name'] = array('like','%'.$_GET['name'].'%');
    //     }
    //     $total=$user->where($where)->count();
        
    //     //实例化分页类
    //     $page=new \Think\Page($total,5);
    //     $this->assign('show',$page->show());
    //     $data=$user->where($where)->page(I('get.p',1),5)->select();
    //     $this->assign('data',$data);
    //     $this->display();
    // }
public function Upload()
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/upload/coach/'; // 设置附件上传根目录
      //  $upload->savePath  =   'Banner/'; // 设置附件上传（子）目录
        $upload->autoSub = false;
        $info = $upload->upload(); // 上传文件

        if (!$info) {
            $this->error($upload->getError());
        } else {
            return $info;
        }
    }
}
